package towerDefense.libraries;

import java.awt.Dimension;
import java.io.File;
import java.util.HashMap;
import java.util.ResourceBundle;
import towerDefense.projectiles.ProjectileQualities;
import util.libraries.ImageToFileNameLibrary;


public class ProjectileLibrary extends ImageToFileNameLibrary
{
    private static final String PROJECTILE_PROPERTIES =
        "towerDefense.libraries.projectiles";
    private static final String DEFAULT_DELIMITER = "\\|";
    HashMap<Integer, ProjectileQualities> myProjectiles;
    Dimension myRangeScalar;


    public ProjectileLibrary (File dir)
    {
        super(dir);
    }


    public ProjectileLibrary (String dir, Dimension dim)
    {
        super(dir, dim);
    }


    @Override
    public void buildLibrary (File file)
    {
        super.buildLibrary(file);
        myProjectiles = new HashMap<Integer, ProjectileQualities>();
        ResourceBundle rb = ResourceBundle.getBundle(PROJECTILE_PROPERTIES);
        for (String key : rb.keySet())
            myProjectiles.put(getIDByName(key),
                              constructProjectile((String) rb.getObject(key)));
    }


    private ProjectileQualities constructProjectile (String s)
    {

        String[] info = s.split(DEFAULT_DELIMITER);
        ProjectileQualities p =
            new ProjectileQualities(info[0],
                                    Integer.parseInt(info[1]),//damage
                                    (int) Double.parseDouble(info[2]) *
                                            (int) Math.sqrt(Math.pow(myRangeScalar.height,
                                                                     2) +
                                                            Math.pow(myRangeScalar.width,
                                                                     2)), //radius
                                    Double.parseDouble(info[3]));//speed
        return p;
    }


    public ProjectileQualities getProjectileByID (int i)
    {
        return myProjectiles.get(i);
    }


    @Override
    public void initialize ()
    {
        super.initialize();
        myRangeScalar = myScalar;
        myScalar = new Dimension(myScalar.width / 2, myScalar.height / 2);

    }

}
